package com.sxj.product.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sxj.base.util.StringUtil;
import com.sxj.conmon.base.BaseController;
import com.sxj.product.application.UserService;
import com.sxj.product.domain.UserInfo;
import com.sxj.product.dto.People;
import com.sxj.product.dto.Person;
import com.sxj.product.dto.UserDTO;
import com.sxj.product.request.UserInfoRequest;
import com.sxj.product.response.UserInfoResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * @author SXJ
 * @Date 2017年12月16日
 */
@RestController
@RequestMapping(value="/user")
@Api(tags = "UserController", description = "用户信息查询")
public class MyController extends BaseController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
    private JavaMailSender mailSender;
	
	@RequestMapping(value="/test", method=RequestMethod.GET)
	@ApiOperation(value = "测试一个页面")
	public ModelAndView test(Map<String, Object> map ,HttpServletRequest request, HttpServletResponse response) {
		People people = People.builder()
				.id("1")
				.name("mytest")
				.age("16").build();
		map.put("people", people);
		return new ModelAndView("index", map);
	}
	
	@RequestMapping(value="/query", method = { RequestMethod.POST })
	@ResponseBody
	@ApiOperation(value = "根据ID查询用户信息")
    public UserInfoResponse queryUserInfo(@RequestBody @Valid UserInfoRequest request) {
		List<UserDTO> dtoList = new ArrayList<>();
		UserInfo info = new UserInfo();
		info.setId(StringUtil.toLongAsNull(request.getId()));
		info.setName(request.getName());
		List<UserInfo> list = userService.queryUserInfo(info);
		if (!CollectionUtils.isEmpty(list)) {
			for (UserInfo userInfo : list) {
				UserDTO dto = getDozerMapper(	).map(userInfo, UserDTO.class);
				dtoList.add(dto);
			}
		}
		UserInfoResponse response = new UserInfoResponse();
		response.setList(dtoList);
        return response;
    }
	
	@RequestMapping(value="/sendMail", method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "发送邮件")
    public void sendEmail(@RequestBody UserInfoRequest request) throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom("Test");
		helper.setTo("18221711953@189.cn");
		helper.setSubject("邮件标题！！！");
		//helper.setText("内容内容！！！");
		helper.setText("<html><head></head><body><h1>hello!!spring html Mail</h1></body></html>", true);
		FileSystemResource pic = new FileSystemResource(new File("C:\\Users\\016403\\Desktop\\22.jpg"));
		helper.addInline("aa.jpg", pic);
//		File file = new File("C:\\Users\\016403\\Desktop\\OrderInfo.pdf");
//		helper.addAttachment("附件.pdf", file);
		mailSender.send(message);
		
//		SimpleMailMessage mail = new SimpleMailMessage();
//		//收件人邮箱地址
//        mail.setTo("sunxiaojie@inner.sss.com");
//        //收件人
//        mail.setFrom("sunxiaojie");
//        //主题
//        mail.setSubject("spring测试发送邮件");
//        //正文
//        mail.setText("hello this mail is from spring javaMail ");
//        //mail.set
//        mailSender.send(mail);
        System.out.println("邮件发送完毕");
	}
	
	@RequestMapping(value="/page", method=RequestMethod.POST)
	@ApiOperation(value = "返回一个页面")
    public ModelAndView index(Map<String, Object> model){
        Person single=new Person("aa",1);
        List<Person> people=new ArrayList<Person>();
        Person p1=new Person("bb",2);
        Person p2=new Person("cc",3);
        Person p3=new Person("dd",4);
        people.add(p1);
        people.add(p2);
        people.add(p3);
        model.put("singlePerson",single);
        model.put("people",people);
        return new ModelAndView("index", model);
    }

}
